
//
//  HomeViewController.swift
//  lt
//
//  Created by AINI on 2019/3/12.
//  Copyright © 2019 cxw. All rights reserved.
//

import UIKit

class UserViewController: UBaseViewController {
    
    var serviceType:Int = 0
    var serviceStatus:Int = 0
    
    var phone: String?
    var userId: Int = 0

    
    
    var myArray: [[[String : String]]] {
        let roleType = UserDefaults.standard.integer(forKey: String.roleType)
        if roleType == 2 {
            return [[["icon":"ico-addr", "title": "我的位置"],
                     ["icon":"ico-coming", "title": "我的收入"],
                     ["icon":"ico-order", "title": "开启/关闭接单"],
                ],
                    [["icon":"ico-flow", "title": "流动服务人员信息"]],
                    [["icon":"ico-faceback", "title": "意见反馈"],
                     ["icon":"ico-safe", "title": "安全中心"],
                ]]
        }else {
            return  [[["icon":"ico-addr", "title": "我的位置"],
                      ["icon":"ico-coming", "title": "我的收入"],
                      ["icon":"ico-order", "title": "开启/关闭接单"],
                ],
                     [["icon":"ico-user", "title": "个人信息"],
                ],
                     [["icon":"ico-faceback", "title": "意见反馈"],
                      ["icon":"ico-safe", "title": "安全中心"],
                ]]
        }
    }
    var authStatus: Int = 0
    
    private lazy var navigationBarY: CGFloat = {
        return navigationController?.navigationBar.frame.maxY ?? 0
    }()
    
    lazy var tableView: UITableView = {
        let el = UITableView(frame: .zero, style: .grouped)
        el.delegate = self
        el.dataSource = self
        let nib = UINib(nibName: "MineCell", bundle: nil)
        el.register(nib, forCellReuseIdentifier: "mineCell")
        let snib = UINib(nibName: "SwitchCell", bundle: nil)
        el.register(snib, forCellReuseIdentifier: "switchCell")
        return el
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.background
        self.tableView.backgroundColor = UIColor.background
        edgesForExtendedLayout = .top
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getUser()
    }
    func configUI(data:StateModel) {
        view.addSubview(tableView)
        tableView.snp.makeConstraints {
            $0.edges.equalTo(self.view.usnp.edges).priority(.low)
            $0.top.equalToSuperview()
        }
        let head: UMineHead = {
            return UMineHead(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 100),userData:data)
        }()
        head.uploadTap = { [weak self] avatar in
          
        }
        tableView.tableHeaderView = head
        
    }

    func getUser() {
//        ApiLoadingProvider.request(UApi.getState(params: ["" : ""]), model: StateModel.self) { (returnData) in
//            if let data = returnData {
//                self.serviceType = data.serviceType
//                self.serviceStatus = data.serviceStatus
//                self.authStatus = data.authStatus
//                self.phone = data.phone
//                UserDefaults.standard.set(self.phone, forKey: String.phone)
//                UserDefaults.standard.set(self.authStatus, forKey: String.authStatus)
//                UserDefaults.standard.set(self.serviceType, forKey: String.roleType)
//                
//                self.configUI(data:data)
//            }
//        }
    }
    //配置导航栏
    
    override func configNavigationBar() {
        super.configNavigationBar()
        navigationController?.navigationBar.isTranslucent = false
        
        let setBtn = UIButton()
        setBtn.setImage(#imageLiteral(resourceName: "icon"), for: .normal)
        setBtn.addTarget(self, action: #selector(goSet), for: .touchUpInside)
        navigationItem.title = "我的"
        let editbutton = UIBarButtonItem(customView: setBtn)
        
        //        navigationController?.setNavigationBarHidden(true, animated: true)
        let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil,
                                     action: nil)
        spacer.width = 20
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: nil,style: .plain,target: nil,action: nil)
        navigationItem.rightBarButtonItems = [spacer,editbutton]
    }
    @objc func goSet() {
        UserDefaults.standard.set("", forKey: String.token)
        self.navigationController?.present(UNavigationController(rootViewController: LoginViewController()), animated: false, completion: nil)
    }
    
}

extension UserViewController: UITableViewDelegate, UITableViewDataSource {
    
    //设置滚动时导航栏样式
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return myArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionArray = myArray[section]
        return sectionArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 6
    }
    
    //配置cell内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let sectionArray = myArray[indexPath.section]
        let dict: [String: String] = sectionArray[indexPath.row]
        
        if indexPath.section == 0 && indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as! SwitchCell
            cell.accessoryType = .none
            cell.selectionStyle = .none
            cell.typeIco.image = UIImage(named: dict["icon"] ?? "")
            cell.typeName.text = dict["title"]
            cell.typeSwitch.isOn = self.serviceStatus == 1 ? false : true
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "mineCell", for: indexPath) as! MineCell
            cell.accessoryType = .disclosureIndicator
            cell.selectionStyle = .none
            cell.typeIco.image = UIImage(named: dict["icon"] ?? "")
            cell.typeName.text = dict["title"]
            return cell
        }
        
    }
    func showLocal(){
//        Toast.toastWait()
        
    }
    func changeState(){
         self.serviceStatus = self.serviceStatus == 1 ? 2 :  1
        self.tableView.reloadData()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch (indexPath.section, indexPath.row) {
        case (0, 0):
            self.showLocal()
        
        case (0, 2):
            self.changeState()
        case (1, 0):
            if self.authStatus != 2 {
                Toast.toastInfo("亲,请先认证~")
                break
            }
            
        default:
            break
        }
        
        
        
        
        
    }
}
